<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>使用range()自动生成外星人 | SongKaBaKa</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="字典嵌套及其修改">
    <meta name="description" content="123456789101112131415161718192021222324252627282930313233alines &#x3D; []for aline_number in range(5):    new_aline &#x3D; &amp;#123;        &#39;color&#39;: &#39;yellow&#39;,        &#39;points&#39;: 10    &amp;#125;    alines.append(new_ali">
<meta property="og:type" content="article">
<meta property="og:title" content="使用range()自动生成外星人">
<meta property="og:url" content="https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/index.html">
<meta property="og:site_name" content="SongKaBaKa">
<meta property="og:description" content="123456789101112131415161718192021222324252627282930313233alines &#x3D; []for aline_number in range(5):    new_aline &#x3D; &amp;#123;        &#39;color&#39;: &#39;yellow&#39;,        &#39;points&#39;: 10    &amp;#125;    alines.append(new_ali">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-07-17T11:02:36.000Z">
<meta property="article:modified_time" content="2020-07-17T11:03:26.309Z">
<meta property="article:author" content="Song Wani">
<meta property="article:tag" content="字典嵌套及其修改">
<meta name="twitter:card" content="summary">
    
    <link rel="shortcut icon" href="/blog/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/brand.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Song Wani</h5>
          <a href="mailto:bugsong@wo.cn" title="bugsong@wo.cn" class="mail">bugsong@wo.cn</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://songbug.gitee.io/blog" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">使用range()自动生成外星人</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="Search">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">使用range()自动生成外星人</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-07-17T11:02:36.000Z" itemprop="datePublished" class="page-time">
  2020-07-17
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E6%A1%88%E4%BE%8B/">案例</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    

<article id="post-使用range()自动生成外星人"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">使用range()自动生成外星人</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-07-17 19:02:36" datetime="2020-07-17T11:02:36.000Z"  itemprop="datePublished">2020-07-17</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E6%A1%88%E4%BE%8B/">案例</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">alines = []</span><br><span class="line"><span class="keyword">for</span> aline_number <span class="keyword">in</span> range(<span class="number">5</span>):</span><br><span class="line">    new_aline = &#123;</span><br><span class="line">        <span class="string">'color'</span>: <span class="string">'yellow'</span>,</span><br><span class="line">        <span class="string">'points'</span>: <span class="number">10</span></span><br><span class="line">    &#125;</span><br><span class="line">    alines.append(new_aline)</span><br><span class="line">print(alines)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 显示前三个外星人</span></span><br><span class="line"><span class="keyword">for</span> aline <span class="keyword">in</span> alines[:<span class="number">3</span>]:</span><br><span class="line">    print(aline)</span><br><span class="line">print(<span class="string">'...'</span>)</span><br><span class="line">print(<span class="string">'一共创建了&#123;&#125;个外星人'</span>.format(str(len(alines))))</span><br><span class="line"><span class="comment"># 由此可见，len在此处获得的是元素个数</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 对前两个元素内容进行修改</span></span><br><span class="line"><span class="keyword">for</span> aline <span class="keyword">in</span> alines[:<span class="number">2</span>]:</span><br><span class="line">    <span class="keyword">if</span> aline[<span class="string">'color'</span>] == <span class="string">'yellow'</span>:  <span class="comment"># 注意这里是等等于</span></span><br><span class="line">        aline[<span class="string">'color'</span>] = <span class="string">'green'</span></span><br><span class="line">        aline[<span class="string">'points'</span>] = <span class="number">15</span></span><br><span class="line">print(alines)  <span class="comment"># 注意alines是一个列表，嵌套了字典</span></span><br><span class="line"><span class="comment"># [&#123;'color': 'green', 'points': 15&#125;, &#123;'color': 'green', 'points': 15&#125;, &#123;'color': 'yellow', 'points': 10&#125;, &#123;'color': 'yellow', 'points': 10&#125;, &#123;'color': 'yellow', 'points': 10&#125;]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 下面是我尝试使用另一种写法来实现修改后面的点数与颜色</span></span><br><span class="line"><span class="keyword">for</span> aline <span class="keyword">in</span> alines[<span class="number">3</span>:]:</span><br><span class="line">    <span class="keyword">for</span> value <span class="keyword">in</span> aline.values():</span><br><span class="line">        <span class="comment"># 这里需要关注，values()方法的返回值是一个元组，我没也并没有直接对不可变类型的元组进行修改，而是继续遍历它，对原alines数据进行修改</span></span><br><span class="line">        <span class="keyword">if</span> value == <span class="string">'yellow'</span>:</span><br><span class="line">            aline[<span class="string">'color'</span>] = <span class="string">'red'</span></span><br><span class="line">            aline[<span class="string">'points'</span>] = <span class="number">20</span></span><br><span class="line">print(alines)</span><br><span class="line"><span class="comment"># [&#123;'color': 'green', 'points': 15&#125;, &#123;'color': 'green', 'points': 15&#125;, &#123;'color': 'yellow', 'points': 10&#125;, &#123;'color': 'red', 'points': 20&#125;, &#123;'color': 'red', 'points': 20&#125;]</span></span><br></pre></td></tr></table></figure>
        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    Last updated: <time datetime="2020-07-17T11:03:26.309Z" itemprop="dateUpdated">2020-07-17 19:03:26</time>
</span><br>


        
        这里可以写作者留言，标签和 hexo 中所有变量及辅助函数等均可调用，示例：<a href="/blog/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/" target="_blank" rel="external">https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/</a>
        
    </div>
    
    <footer>
        <a href="https://gitee.com/songbug/blog.git">
            <img src="/blog/img/avatar.jpg" alt="Song Wani">
            Song Wani
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/%E5%AD%97%E5%85%B8%E5%B5%8C%E5%A5%97%E5%8F%8A%E5%85%B6%E4%BF%AE%E6%94%B9/" rel="tag">字典嵌套及其修改</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/&title=《使用range()自动生成外星人》 — SongKaBaKa&pic=https://gitee.com/songbug/blog.git/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/&title=《使用range()自动生成外星人》 — SongKaBaKa&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《使用range()自动生成外星人》 — SongKaBaKa&url=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/&via=https://gitee.com/songbug/blog.git" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/2020/09/22/HEXO%E7%8E%AF%E5%A2%83%E7%9A%84%E6%90%AD%E5%BB%BA/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">HEXO环境的搭建</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Python常用方法及函数</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/blog/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/blog/img/wechat.jpg" data-alipay="/blog/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
            <span>This blog is licensed under a <a rel="license noopener" href="https://creativecommons.org/licenses/by/4.0/" target="_blank">Creative Commons Attribution 4.0 International License</a>.</span>
        </p>
    </div>
    <div class="bottom">
        <p><span>Song Wani &copy; 2019 - 2021</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/&title=《使用range()自动生成外星人》 — SongKaBaKa&pic=https://gitee.com/songbug/blog.git/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/&title=《使用range()自动生成外星人》 — SongKaBaKa&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《使用range()自动生成外星人》 — SongKaBaKa&url=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/&via=https://gitee.com/songbug/blog.git" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://gitee.com/songbug/blog.git/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
